<?php
class Category extends MY_Controller
{
	private $error = array();
	
	function __construct()
	{
		parent::__construct('category');
	}
	
	function index()
	{
		$this->lang->load('catalog/category');
		
		//添加列表所需的js和css文档
		$this->document->setTitle($this->lang->line('heading_title'));
		$script = base_url()."includes/js/easyui/jquery.easyui.min.js";
		$this->document->addScript($script);
		
		$style_one = base_url()."includes/js/easyui/themes/default/easyui.css";
		$style_two = base_url()."includes/js/easyui/themes/icon.css";
		
		$this->document->addStyle($style_one);
		$this->document->addStyle($style_two);
		
		
		
		$this->load->model('catalog/Model_Category');
		
   		$data['breadcrumbs'] = array();

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('text_home'),
			'href'      => site_url().'/common/home',
      		'separator' => false
   		);
		
		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('heading_title'),
			'href'      => site_url().'/catalog/category',
      		'separator' => "::"
   		);
									
		$data['insert'] = site_url().'/catalog/category/insert';
		$data['delete'] = site_url().'/catalog/category/delete';
		$data['edit'] = site_url().'/catalog/category/update';
		
		$data['datagrid'] = site_url().'/catalog/category/datagrid';
		
		$data['heading_title'] = $this->lang->line('heading_title');

		$data['text_no_results'] = $this->lang->line('text_no_results');

		$data['column_name'] = $this->lang->line('column_name');
		$data['column_sort_order'] = $this->lang->line('column_sort_order');
		$data['column_status'] = $this->lang->line('column_status');
		$data['column_action'] = $this->lang->line('column_action');
		
		$data['message_selections'] = $this->lang->line('message_selections');

		$data['button_insert'] = $this->lang->line('button_insert');
		$data['button_delete'] = $this->lang->line('button_delete');
		$data['button_edit'] = $this->lang->line('button_edit');
 
 		if ($this->session->userdata('warning')) {
			$data['error_warning'] = $this->session->userdata('warning');
		} else {
			$data['error_warning'] = '';
		}
		
		
		
		
		if ($this->session->userdata('success')) {
			$data['success'] = $this->session->userdata('success');
			$this->session->unset_userdata('success');
		} else {
			$data['success'] = '';
		}
		
		//$data['header'] = helper_load_header();
		//$data['footer'] = helper_load_footer();
		
		$this->load->view('catalog/category_manage', $data);
	
		
		
	}
	
	function datagrid()
	{
		$this->lang->load('catalog/category');		
		$this->load->model('catalog/Model_Category');
		$this->load->model('setting/Model_Config');
		
		$data['heading_title'] = $this->lang->line('heading_title');

		$data['text_no_results'] = $this->lang->line('text_no_results');
		
		

		$data['column_name'] = $this->lang->line('column_name');
		$data['column_sort_order'] = $this->lang->line('column_sort_order');
		$data['column_status'] = $this->lang->line('column_status');
		$data['column_action'] = $this->lang->line('column_action');

		$data['button_insert'] = $this->lang->line('button_insert');
		$data['button_delete'] = $this->lang->line('button_delete');
		
		
		
		
		
		if($this->input->post('page'))
		{
			$page = (int)$this->input->post('page');
			$rows = (int)$this->input->post('rows');
		}
		else
		{
			$page = 1;
			$rows = 10;
		}
		
		$array_one = array();
		
		
		
			$sort = $this->input->post('sort');
			
			
			
			
			switch($sort)
			{
				case "name":
				
					$sortone = "category_description.name ".$this->input->post('order');
					break;
				case "sort_order":
					$sortone = "category.sort_order ".$this->input->post('order');
					break;
				case "status":
					$sortone = "category.status ".$this->input->post('order');
					break;
				
				default:
					$sortone = "category.lft ".$this->input->post('order');
					
			}
			
			
		$arrayone = array();
		$keyarray = array();	
			
		
		$offset = $rows*($page-1);
		$num = $rows;
		
		$language_id = $this->Model_Config->getLanguageID($this->Model_Config->getKey('config_admin_language'));
		//$language_id = $this->Model_Config->getLanguageID('en');
		
		$dataone = $this->Model_Category->get_specific_all($rows, $offset, $sortone, $keyarray, $language_id);
		
		foreach($dataone->result() as $one)
		{
			if($one->status == 1)
			{
				$status = "Enabled";
			}
			else
			{
				$status = "Disabled";
			}
			$array_one[] = array(
				"category_id" => $one->category_id,
				"name" => $this->Model_Category->get_fullpath_name($one->category_id, ' -> '),
				"parent_id" => $one->parent_id,
				"status" => $status,
				"sort_order" => $one->sort_order,
				"lft" => $one->lft,				
				
			);
			
			
		}
		
		$total_num = $this->Model_Category->get_specific_count_all($keyarray);
		
		
		$array = array(
			"total" => $total_num,
			"rows" => $array_one,
		);
		
		print_r(json_encode($array));
		
		
	
	}
	



	function insert() {
		$this->lang->load('catalog/category');
		$this->load->model('setting/Model_Config');

		$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('catalog/Model_Category');
		
		//如果提交且通过了验证，则保存并转向到列表页面
		if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
			$this->Model_Category->addCategory($this->input->post());

			$this->session->set_userdata('success', $this->lang->line('text_success'));
			
			redirect('catalog/category'); 
		}

		$this->getForm();
	}
	
	function update() {
		$this->lang->load('catalog/category');
		$this->load->model('setting/Model_Config');

		$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('catalog/Model_Category');
		
		if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validateForm()) {
			$this->Model_Category->editCategory($this->uri->segment(4), $this->input->post());
			
			$this->session->set_userdata('success', $this->lang->line('text_success'));
			
			redirect(site_url().'/catalog/category');
		}

		$this->getForm();
	}
	
	
	
	function getForm() {
		$data['heading_title'] = $this->lang->line('heading_title');

		$data['text_none'] = $this->lang->line('text_none');
		$data['text_default'] = $this->lang->line('text_default');
		$data['text_image_manager'] = $this->lang->line('text_image_manager');
		$data['text_enabled'] = $this->lang->line('text_enabled');
    	$data['text_disabled'] = $this->lang->line('text_disabled');
		$data['text_percent'] = $this->lang->line('text_percent');
		$data['text_amount'] = $this->lang->line('text_amount');
				
		$data['entry_name'] = $this->lang->line('entry_name');
		$data['entry_meta_keyword'] = $this->lang->line('entry_meta_keyword');
		$data['entry_meta_description'] = $this->lang->line('entry_meta_description');
		$data['entry_description'] = $this->lang->line('entry_description');
		$data['entry_store'] = $this->lang->line('entry_store');
		$data['entry_keyword'] = $this->lang->line('entry_keyword');
		$data['entry_parent'] = $this->lang->line('entry_parent');
		$data['entry_image'] = $this->lang->line('entry_image');
		$data['entry_top'] = $this->lang->line('entry_top');
		$data['entry_column'] = $this->lang->line('entry_column');		
		$data['entry_sort_order'] = $this->lang->line('entry_sort_order');
		$data['entry_status'] = $this->lang->line('entry_status');
		$data['entry_layout'] = $this->lang->line('entry_layout');
		
		$data['button_save'] = $this->lang->line('button_save');
		$data['button_cancel'] = $this->lang->line('button_cancel');

    	$data['tab_general'] = $this->lang->line('tab_general');
    	$data['tab_data'] = $this->lang->line('tab_data');
		$data['tab_design'] = $this->lang->line('tab_design');
		
 		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
	
 		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		} else {
			$data['error_name'] = array();
		}

  		$data['breadcrumbs'] = array();

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('text_home'),
			'href'      => site_url().'/common/home',
      		'separator' => false
   		);

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->lang->line('heading_title'),
			'href'      => site_url().'/catalog/category',
      		'separator' => ' :: '
   		);
		
		
		
		if ((int)$this->uri->segment(4) == 0) {
			$data['action'] = site_url().'/catalog/category/insert';
		} else {
			$data['action'] = site_url().'/catalog/category/update/'.$this->uri->segment(4);
		}
		
		$data['cancel'] = site_url().'/catalog/category';


		if (((int)$this->uri->segment(4) != 0) && ($this->input->server('REQUEST_METHOD') != 'POST')) {
      		$category_info = $this->Model_Category->getCategory($this->uri->segment(4));
			
    	}
		
		$this->load->model('localisation/Model_Language');
		
		$data['languages'] = $this->Model_Language->getLanguages();

		if ($this->input->post('category_description')) {
			$data['category_description'] = $this->input->post('category_description');
		} elseif (isset($category_info)) {
			$data['category_description'] = $this->Model_Category->getCategoryDescriptions($this->uri->segment(4));
		} else {
			$data['category_description'] = array();
		}

		$categories = $this->Model_Category->getCategories(0);

		// Remove own id from list
		if (isset($category_info)) {
			foreach ($categories as $key => $category) {
				if ($category['category_id'] == $category_info['category_id']) {
					unset($categories[$key]);
				}
			}
		}

		$data['categories'] = $categories;

		if ($this->input->post('parent_id')) {
			$data['parent_id'] = $this->input->post('parent_id');
		} elseif (isset($category_info)) {
			$data['parent_id'] = $category_info['parent_id'];
		} else {
			$data['parent_id'] = 0;
		}
						
		$this->load->model('setting/Model_Store');
		
		$data['stores'] = $this->Model_Store->getStores();
		
		if ($this->input->post('category_store')) {
			$data['category_store'] = $this->input->post('category_store');
		} elseif (isset($category_info)) {
			$data['category_store'] = $this->Model_Category->getCategoryStores($this->uri->segment(4));
		} else {
			$data['category_store'] = array(0);
		}			
		
		if ($this->input->post('keyword')) {
			$data['keyword'] = $this->input->post('keyword');
		} elseif (isset($category_info)) {
			$data['keyword'] = $category_info['keyword'];
		} else {
			$data['keyword'] = '';
		}

		if ($this->input->post('image')) {
			$data['image'] = $this->input->post('image');
		} elseif (isset($category_info)) {
			$data['image'] = $category_info['image'];
		} else {
			$data['image'] = '';
		}
		
		$this->load->model('tool/Model_Image');

		if (isset($category_info) && $category_info['image'] && file_exists(DIR_IMAGE . $category_info['image'])) {
			$data['preview'] = $this->Model_Image->resize($category_info['image'], 100, 100);
		} else {
			$data['preview'] = $this->Model_Image->resize('no_image.jpg', 100, 100);
		}
		
		
		
		if ($this->input->post('top')) {
			$data['top'] = $this->input->post('top');
		} elseif (isset($category_info)) {
			$data['top'] = $category_info['top'];
		} else {
			$data['top'] = 0;
		}
		
		if ($this->input->post('column')) {
			$data['column'] = $this->input->post('column');
		} elseif (isset($category_info)) {
			$data['column'] = $category_info['column'];
		} else {
			$data['column'] = 1;
		}
				
		if ($this->input->post('sort_order')) {
			$data['sort_order'] = $this->input->post('sort_order');
		} elseif (isset($category_info)) {
			$data['sort_order'] = $category_info['sort_order'];
		} else {
			$data['sort_order'] = 0;
		}
		
		if ($this->input->post('status')) {
			$data['status'] = $this->input->post('status');
		} elseif (isset($category_info)) {
			$data['status'] = $category_info['status'];
		} else {
			$data['status'] = 1;
		}
				
		if ($this->input->post('category_layout')) {
			$data['category_layout'] = $this->input->post('category_layout');
		} elseif (isset($category_info)) {
			$data['category_layout'] = $this->Model_Category->getCategoryLayouts($this->uri->segment(4));
		} else {
			$data['category_layout'] = array();
		}

		$this->load->model('design/Model_Layout');
		
		$data['layouts'] = $this->Model_Layout->getLayouts();
		
		$data['header'] = helper_load_header();
		$data['footer'] = helper_load_footer();
						
		$this->load->view('catalog/category_form', $data);
	}

	function validateForm() {
		//判断是否有权限进行插入和编辑，否则报错
		if (!$this->Model_User->has_permission('category_edit', $this->session->userdata('user_id'))) {
			$this->error['warning'] = $this->lang->line('error_permission');
		}
		
		//检查所输入的分类名称是否在2到255字符之间
		foreach ($this->input->post('category_description') as $language_id => $value) {
			if ((strlen(utf8_decode($value['name'])) < 2) || (strlen(utf8_decode($value['name'])) > 255)) {
				$this->error['name'][$language_id] = $this->lang->line('error_name');
			}
		}
		
		//如果有错误，但不是警告信息，则将警告信息赋值
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->lang->line('error_warning');
		}
					
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	function validateDelete() {
		if (!$this->Model_User->has_permission('category_delete', $this->session->userdata('user_id'))) {
			$this->error['warning'] = $this->lang->line('error_permission');
		}
 
		if (!$this->error) {
			return true; 
		} else {
			return false;
		}
	}
	
	



	function delete()
	{
		$success = false;
		$this->load->model('catalog/Model_Category');
		$category_ids = $this->input->post('category_ids');
		
		$cid = explode('-', $category_ids);
		
		if ($cid && $this->validateDelete()) {
			for($i=0; $i<count($cid); $i++)
			{
				$success = $this->Model_Category->deleteCategory($cid[$i]);
			}
			
			if($success)
			{
				echo "succ";
			}
			else
			{
				echo "fail1";
			}
		}
		else
		{
			echo "fail2";
		}
		
	}
	
	
	
}
?>